//
//  WelcomeController.swift
//  JJWeibo
//
//  Created by JJ on 15/9/6.
//  Copyright © 2015年 JJ. All rights reserved.
//

import UIKit
import SDWebImage

class WelcomeController: UIViewController {

    private var iconBottomCons:NSLayoutConstraint?
    override func viewDidLoad() {
        super.viewDidLoad()

        setUI()
        iconView.sd_setImageWithURL(UserAccountViewModel.sharedUserAccount.avatarUrl)
    }
    
    override func viewDidAppear(animated: Bool) {
        super.viewDidAppear(animated)
        let h = -(UIScreen.mainScreen().bounds.height + iconBottomCons!.constant)
        iconBottomCons?.constant=h
        
        UIView.animateWithDuration(1.2, delay: 0.0, usingSpringWithDamping: 0.8, initialSpringVelocity: 10, options: [], animations: { () -> Void in
            self.view.layoutIfNeeded()
            }) { (_) -> Void in
                NSNotificationCenter.defaultCenter().postNotificationName(JJSwitchRootViewControllerNotification, object: nil)
        }
        
    }
    
    private func setUI(){
        view.addSubview(backImageView)
        view.addSubview(iconView)
        view.addSubview(lable)
        backImageView.translatesAutoresizingMaskIntoConstraints=false
        view.addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("H:|-0-[v]-0-|", options: [], metrics:nil, views: ["v":backImageView]))
        view.addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("V:|-0-[v]-0-|", options: [], metrics: nil, views: ["v":backImageView]))
        
        iconView.translatesAutoresizingMaskIntoConstraints=false
        view.addConstraint(NSLayoutConstraint(item: iconView, attribute: NSLayoutAttribute.CenterX, relatedBy: NSLayoutRelation.Equal, toItem: view, attribute: NSLayoutAttribute.CenterX, multiplier: 1.0, constant: 0))
        view.addConstraint(NSLayoutConstraint(item: iconView, attribute: NSLayoutAttribute.Bottom, relatedBy: NSLayoutRelation.Equal, toItem: view, attribute: NSLayoutAttribute.Bottom, multiplier: 1.0, constant: -200))
        iconBottomCons=view.constraints.last
        view.addConstraint(NSLayoutConstraint(item: iconView, attribute: NSLayoutAttribute.Width, relatedBy: NSLayoutRelation.Equal, toItem: nil, attribute: NSLayoutAttribute.NotAnAttribute, multiplier: 1.0, constant: 90))
        view.addConstraint(NSLayoutConstraint(item: iconView, attribute: NSLayoutAttribute.Height, relatedBy: NSLayoutRelation.Equal, toItem: nil, attribute: NSLayoutAttribute.NotAnAttribute, multiplier: 1.0, constant: 90))
        
        lable.translatesAutoresizingMaskIntoConstraints=false
        view.addConstraint(NSLayoutConstraint(item: lable, attribute: NSLayoutAttribute.CenterX, relatedBy: NSLayoutRelation.Equal, toItem: view, attribute: NSLayoutAttribute.CenterX, multiplier: 1.0, constant: 0))
        view.addConstraint(NSLayoutConstraint(item: lable, attribute: NSLayoutAttribute.Top, relatedBy: NSLayoutRelation.Equal, toItem: iconView, attribute: NSLayoutAttribute.Bottom, multiplier: 1.0, constant: 16))
        
    }
    private lazy var backImageView:UIImageView=UIImageView(image: UIImage(named: "ad_background"))
    
    private lazy var lable:UILabel={
       let lab=UILabel()
        lab.text="Welcome Back Hero"
        return lab
    }()
    
    private lazy var iconView:UIImageView={
        let iv=UIImageView(image: UIImage(named: "avatar_default_big"))
        iv.layer.cornerRadius=45
        iv.layer.masksToBounds=true
        return iv
    }()
}
